/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

/**
 *
 * @author Jack
 */
import Database.ConnectToDatabase;
import java.sql.*;
import java.util.*;

public class Produce {

    private int idproduce;
    private String nameproduce;
    private double price;
    private int warranty;
    private String infor;
    private int idproducer;

    public Produce() {
    }

    /**
     * @return the idproduce
     */
    public int getIdproduce() {
        return idproduce;
    }

    /**
     * @param idproduce the idproduce to set
     */
    public void setIdproduce(int idproduce) {
        this.idproduce = idproduce;
    }

    /**
     * @return the nameproduce
     */
    public String getNameproduce() {
        return nameproduce;
    }

    /**
     * @param nameproduce the nameproduce to set
     */
    public void setNameproduce(String nameproduce) {
        this.nameproduce = nameproduce;
    }

    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }

    /**
     * @return the warranty
     */
    public int getWarranty() {
        return warranty;
    }

    /**
     * @param warranty the warranty to set
     */
    public void setWarranty(int warranty) {
        this.warranty = warranty;
    }

    /**
     * @return the infor
     */
    public String getInfor() {
        return infor;
    }

    /**
     * @param infor the infor to set
     */
    public void setInfor(String infor) {
        this.infor = infor;
    }

    /**
     * @return the idproducer
     */
    public int getIdproducer() {
        return idproducer;
    }

    /**
     * @param idproducer the idproducer to set
     */
    public void setIdproducer(int idproducer) {
        this.idproducer = idproducer;
    }

    public static String ListTName(int a) {
        String liste = "";
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select nameproduce from produce,dispatchno where dispatchno.idproduce=produce.idproduce and dispatchno.iddispatch=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                liste = rs.getString("nameproduce");
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static int ListIdProduce(int a) {
        int liste = -1;
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select idproduce from dispatchno where  dispatchno.iddispatch=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                liste = rs.getInt("idproduce");
            }
        } catch (Exception ex) {
            liste = -1;
        }
        return liste;
    }

    public static Vector<Produce> ListIDProducec() {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Produce" + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Produce> ListIDProducecOnId(int idproduce) {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Produce where idproduce = " + idproduce + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static String nameproduce(int a) {
        String listidpd = new String();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select nameproduce from Produce,dispatchno where dispatchno.idproduce=produce.idproduce and dispatchno.idproduce=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                listidpd = rs.getString("nameproduce");
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static String nameproduces(int a) {
        String listidpd = new String();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select nameproduce from Produce where produce.idproduce=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                listidpd = rs.getString("nameproduce");
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Produce> ListProduceonnameproducer(String a) {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select idproduce,nameproduce,price,warranty,information,produce.idproducer from Produce,producer where produce.idproducer=producer.idproducer and nameproducer like '%" + a + "%'" + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Produce> ListProduceonidproduce(int a) {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Produce where idproduce=" + a + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Produce> ListProduceonnameproduce(String a) {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select idproduce,nameproduce,price,warranty,information,produce.idproducer from Produce,producer where produce.idproducer=producer.idproducer and  nameproduce like'%" + a + "%'" + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Produce> ListProduceonIDproducer(int a) {
        Vector<Produce> listidpd = new Vector<Produce>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select idproduce,nameproduce,price,warranty,information,produce.idproducer from Produce,producer where produce.idproducer=producer.idproducer and  produce.idproducer=" + a + " order by idproduce desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Produce pd = new Produce();
                pd.setIdproduce(rs.getInt("idproduce"));
                pd.setNameproduce(rs.getString("nameproduce"));
                pd.setPrice(rs.getFloat("price"));
                pd.setWarranty(rs.getInt("warranty"));
                pd.setInfor(rs.getString("information"));
                pd.setIdproducer(rs.getInt("idproducer"));
                listidpd.add(pd);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public boolean AddProduce() {
        return CommonQuery.Command("insert into Produce values('" + this.nameproduce + "', " + this.price + ", " + this.warranty + ", '" + this.infor + "', " + this.idproducer + ")");
    }

    public boolean UpdateProduce() {
        return CommonQuery.Command("update Produce  set nameproduce='" + this.nameproduce + "',price=" + this.price + ",warranty=" + this.warranty + ", information='" + this.infor + "', idproducer=" + this.idproducer + "  where idproduce=" + this.idproduce);
    }

    public boolean DeleteProduce() {
        return CommonQuery.Command("delete Produce where idproduce=" + this.idproduce);
    }
}
